<template>
  <div class="edit-shop-base-wrap">
    <div class="header flex-c">
      <p class="title">企业注册信息</p>
      <a-button size="mini" style="margin-right: 20px" @click="reset">重置</a-button>
      <a-button type="primary" size="mini" @click="saveShop" :loading="summitLoading">保存</a-button>
    </div>
    <a-form :model="formInfo">
      <a-row :gutter="16">
        <a-col :span="11">
          <a-form-item label="企业名称：" label-col-flex="130px">
            <a-input v-model="formInfo.companyName" placeholder="" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="11">
          <a-form-item label="统一社会信用代码：" label-col-flex="130px">
            <a-input v-model="formInfo.socialCreditId" placeholder="" allow-clear />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="11">
          <a-form-item label="法人(中文)：" label-col-flex="130px">
            <a-input v-model="formInfo.legalPerson" placeholder="" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="11">
          <a-form-item label="法人身份证号：" label-col-flex="130px">
            <a-input v-model="formInfo.legalPersonId" placeholder="" allow-clear />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="22">
          <a-form-item label="相关资料：" label-col-flex="130px">
            <div class="flex-c-sb" style="width: 100%">
              <ul class="flex-wrap">
                <li class="file-item" v-for="(item, i) in formInfo.information" :key="i">
                  <a-image width="60" fit="contain" :src="item.fileUrl" />
                  <p class="name">{{ item.fileName }}</p>
                  <icon-delete class="delete-icon" />
                </li>
              </ul>
              <a-popover trigger="click" title="上传资料" ref="boxWrap">
                <a-button size="mini" type="primary">上传资料</a-button>
                <template #content>
                  <div class="update-wrap">
                    <a-input style="margin: 20px 0; width: 200px" v-model="fileName" placeholder="请输入文件名称" />
                    <div class="flex-c">
                      <ul>
                        <li class="pic-wrap" v-for="(file, i) in fileList" :key="i">
                          <a-image width="80" fit="contain" :src="file" />
                        </li>
                      </ul>
                      <a-upload list-type="picture-card" action="/" ref="uploadRef" :custom-request="uplaodFile" :show-file-list="false" image-preview />
                    </div>
                    <a-space>
                      <a-button size="mini" @click="cancelUpload">取消</a-button>
                      <a-button type="primary" size="mini" @click="submitFile">确认</a-button>
                    </a-space>
                  </div>
                </template>
              </a-popover>
            </div>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { updateShopEnterprise, getShopInfo } from '@/api/system'
  import { uplaodImg } from '@/api/base'
  import { Message } from '@arco-design/web-vue'
  onMounted(async () => {
    console.log(props.currentData)
    setFormInfo(props.currentData)
  })
  const props = defineProps({
    currentData: {
      type: Object,
      default: () => {},
    },
    shopId: {
      type: String,
    },
  })

  const formInfo: any = ref({})
  let defaultInfo: any = JSON.parse(JSON.stringify(props.currentData))
  const summitLoading = ref(false)
  const fileName = ref()
  const fileList = ref<Array<any>>([])
  const boxWrap = ref()
  const uploadRef = ref()

  function setFormInfo(currentData: any) {
    formInfo.value = JSON.parse(JSON.stringify(currentData))
  }

  // 获取店铺详情
  async function getShopDetail() {
    const res: any = await getShopInfo({
      id: props.shopId,
    })
    defaultInfo = res?.data?.shopEnterprise || {}
    setFormInfo(defaultInfo)
  }
  // 重置信息
  function reset() {
    setFormInfo(defaultInfo)
  }
  // 取消上传
  function cancelUpload() {
    fileList.value = []
    fileName.value = null
    boxWrap.value.handlePopupVisibleChange(false)
  }
  const uplaodFile: any = async ({ fileItem }) => {
    let formData = new FormData()
    formData.append('file', fileItem.file)
    const res: any = await uplaodImg(formData)
    if (res?.data?.publicUrl) {
      fileList.value.push(res.data.publicUrl)
    }
  }

  // 上传
  async function submitFile() {
    if (!fileName.value) {
      return Message.error('请输入文件名称')
    }
    if (!fileList.value.length) {
      return Message.error('请选择文件')
    }
    fileList.value.forEach((file) => {
      if (!formInfo.value.information) formInfo.value.information = []
      formInfo.value.information.push({
        fileName: fileName.value,
        fileUrl: file,
      })
    })
    cancelUpload()
  }
  // 提交企业信息
  async function saveShop() {
    summitLoading.value = true
    const res: any = await updateShopEnterprise({
      ...formInfo.value,
      id: defaultInfo.id,
      shopId: props.shopId,
    })
    summitLoading.value = false
    if (res) {
      Message.success('保存成功！')
      getShopDetail()
    }
  }
</script>
<style lang="less" scoped>
  .header {
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 20px;
    border-left: 3px solid rgba(var(--primary-6), 0.5);
    padding-left: 10px;
    .title {
      margin-right: 30px;
      width: 90px;
    }
  }
  .update-wrap {
    min-width: 400px;
  }
  .arco-space {
    width: 100%;
    justify-content: flex-end;
    margin: 20px 0;
  }
  .pic-wrap {
    margin-right: 10px;
    border: 1px dashed var(--color-neutral-3);
    border-radius: var(--border-radius-small);
  }
  .file-item {
    margin-right: 10px;
    position: relative;
    .arco-image {
      border: 1px dashed var(--color-neutral-3);
      width: 60px;
      height: 60px;
    }
    .name {
      font-size: 12px;
      text-align: center;
    }
    .delete-icon {
      position: absolute;
      top: 0;
      right: 0;
      z-index: 10;
      color: #fff;
      background-color: rgba(0, 0, 0, 0.5);
      cursor: pointer;
    }
  }
  .arco-form-item {
    margin-bottom: 10px;
  }
  :deep(.arco-form-item-label-col) {
    padding-right: 5px;
  }
</style>
